package com.sky.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Map;

/**
 * @projectName: netty-cloud
 * @package: com.sky.controller
 * @className: FileController
 * @author: zhl
 * @description: TODO
 * @date: 2024/1/24 14:04
 * @version: 1.0
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @GetMapping("/getFile")
    @PreAuthorize("hasAuthority('USER')")
    public String getFile(String fileName){
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        // 用户身份信息
        Object principal = authentication.getPrincipal();
        // 用户凭证
        Object credentials = authentication.getCredentials();
        // 用户权限信息
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        Map map = JSON.parseObject(JSON.toJSONString(principal), Map.class);
        return "上传" + map.get("username").toString() + "文件成功";
    }
}
